package com.bjtang;

import com.bjtang.Commom.osSelect;
import com.bjtang.NetSDKDemo.HCNetSDK;
import com.sun.jna.Native;
import com.sun.jna.Pointer;


import org.json.JSONException;

import java.io.UnsupportedEncodingException;

/**
 * 明眸门禁以卡为中心demo示例
 */
public class AcsMain {
	static HCNetSDK hCNetSDK = null;
	static int lUserID = -1;// 用户句柄
	static int iCharEncodeType = 0; // 设备字符集


	/**
	 * 根据不同操作系统选择不同的库文件和库路径
	 * 
	 * @return
	 */
	private static boolean createSDKInstance() {
		if (hCNetSDK == null) {
			synchronized (HCNetSDK.class) {
				String strDllPath = "";
				try {
					// System.setProperty("jna.debug_load", "true");
					if (osSelect.isWindows())
						// win系统加载库路径
						strDllPath = System.getProperty("user.dir") + "\\lib\\windows\\HCNetSDK.dll";
					else if (osSelect.isLinux())
						// Linux系统加载库路径
						strDllPath = "/usr/lib/libhcnetsdk.so";
					hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
				} catch (Exception ex) {
					System.out.println("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException, JSONException {
		if (hCNetSDK == null) {
			if (!createSDKInstance()) {
				System.out.println("Load SDK fail");
				return;
			}
		}
		// linux系统建议调用以下接口加载组件库
		if (osSelect.isLinux()) {
			HCNetSDK.BYTE_ARRAY ptrByteArray1 = new HCNetSDK.BYTE_ARRAY(256);
			HCNetSDK.BYTE_ARRAY ptrByteArray2 = new HCNetSDK.BYTE_ARRAY(256);
			// 这里是库的绝对路径，请根据实际情况修改，注意改路径必须有访问权限
			String strPath1 = "/usr/lib/libcrypto.so.1.1";
			String strPath2 = "/usr/lib/libssl.so.1.1";

			System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
			ptrByteArray1.write();
			hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());

			System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
			ptrByteArray2.write();
			hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());

			String strPathCom = System.getProperty("user.dir") + "/lib/linux";
			HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
			System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
			struComPath.write();
			hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
		}

		/** 初始化 */
		hCNetSDK.NET_DVR_Init();

		/** 加载日志 */
		boolean i = hCNetSDK.NET_DVR_SetLogToFile(3, "./sdklog", false);

	
		/** 登录 */
	//	login_V40("www.bjtang.com", "admin", "hik12345", (short) 7002);
		if(		login_V40(args[0], args[1], args[2], (short) 	Integer.valueOf(args[3]).intValue())) {
			EventSearch.searchAllEvent(lUserID,	args[4]);
			Thread.sleep(15000);

		}

		/** 查询设备事件 */

		/**
		 * 增加sleep时间，保证程序一直运行，
		 */

		/** 登出操作 */
		AcsMain.logout();

		// 释放SDK，程序退出前调用
		hCNetSDK.NET_DVR_Cleanup();
		return;
	}

	/**
	 * 设备登录
	 * 
	 * @param ipadress IP地址
	 * @param user     用户名
	 * @param psw      密码
	 * @param port     端口，默认8000
	 */
	public static boolean login_V40(String ipadress, String user, String psw, short port) {
		// 注册
		HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();// 设备登录信息
		String m_sDeviceIP = ipadress;// 设备ip地址
		m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
		System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
		String m_sUsername = user;// 设备用户名
		m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
		System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
		String m_sPassword = psw;// 设备密码
		m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
		System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
		m_strLoginInfo.wPort = port; // sdk端口
		m_strLoginInfo.bUseAsynLogin = false; // 是否异步登录：0- 否，1- 是
		m_strLoginInfo.write();
		HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();// 设备信息
		lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
		if (lUserID == -1) {
			System.out.println("登录失败，错误码为" + hCNetSDK.NET_DVR_GetLastError());
			return false;
		} else {
			System.out.println("登录成功！");
			m_strDeviceInfo.read();
			iCharEncodeType = m_strDeviceInfo.byCharEncodeType;
			return true;

		}
	}

	/**
	 * 登出操作
	 *
	 */
	public static void logout() {

		/** 登出和清理，释放SDK资源 */
		if (lUserID >= 0) {
			hCNetSDK.NET_DVR_Logout(lUserID);
		}

	}

}
